<?php

class PaisHelper {
    public static function paises(){
        return Config::get('paises');
    }
    
    public static function pais($pais = null){
        // Informa o cambia el pais
        
        if(is_null($pais)){
            $pais = Session::get('pais');
            return empty($pais) ? "argentina" : $pais;
        } else {
            $paises = static::paises();
            if(isset($paises[$pais])){
                Session::set('pais', $paises[$pais]);
            }
        }
    }
    
    public static function select($echo = false, $options = array()){
        $paises = static::paises();
        
        $actual = !isset($options['selected'])?static::pais():$options['selected'];
        $name = empty($options['name'])?' ':' name="'.$options['name'].'" ';
        $empty_option = empty($options['empty_option'])?'':'<option value="" >Indistinto</option>'."\n";
        $class = !isset($options['class'])?'selector_pais':$options['class'];
        
        $s = "<select class=\"".$class."\"".$name.">\n";
        
        $s = $s . $empty_option;
        foreach($paises as $etiqueta => $pais){
            $s = $s . "<option value=\"$etiqueta\"". ($paises[$etiqueta] == $actual?' selected="selected"':'') .">".$pais."</option>\n";
        }
        
        $s = $s . "</select>\n";
        
        if($echo) echo $s;
        
        return $s;
    }

    public static function select_flags($echo = false, $options = array()){
        $paises = static::paises();

        $actual = !isset($options['selected'])?static::pais():$options['selected'];
        $name = empty($options['name'])?' ':' name="'.$options['name'].'" ';
        $empty_option = empty($options['empty_option'])?'':'<option value="" >Indistinto</option>'."\n";
        $class = !isset($options['class'])?'selector_pais':$options['class'];

        $s = "<select class=\"".$class."\"".$name.">\n";

        $s = $s . $empty_option;
        $s = $s . "<option value=''>SELECCIONE UN PAÍS</option>\n";
        foreach($paises as $etiqueta => $pais){
            $s = $s . "<option value=\"$etiqueta\"". ($paises[$etiqueta] == $actual?' selected="selected"':'') ." title='/imagenes/landing/flags/".$etiqueta.".jpg'>".$pais."</option>\n";
        }

        $s = $s . "</select>\n";

        if($echo) echo $s;

        return $s;
    }

    public static function checkboxProducto($paises){
        $o = '';
        $todos = static::paises();
        foreach((array)$todos as $etiqueta => $pais){
          $o .= '<div class="pais_check_div"><input type="checkbox" class="pais_check" name="p[]" '.(in_array($etiqueta, $paises)?' checked="checked" ':'').'value="'.$etiqueta.'"> '.$pais.'</div>'."\n";  
        }
        return $o;
    }
}